﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Web.Security;

namespace ProjectSem3.Admin
{
    public partial class WebForm5 : System.Web.UI.Page
    {
        private dataDataContext dc = new dataDataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
            Employee emp = (Employee)Session["user"];
            if (emp != null && emp.RoleEmID == 1)
            {
                if (!IsPostBack)
                {

                    ddlSupplier.DataSource = dc.Suppliers;
                    ddlSupplier.DataTextField = "Name";
                    ddlSupplier.DataValueField = "SupplierID";
                    ddlSupplier.DataBind();

                    ddlCategory.DataSource = dc.Categories;
                    ddlCategory.DataTextField = "Name";
                    ddlCategory.DataValueField = "CategoryID";
                    ddlCategory.DataBind();
                }
            }
            else
                Button1.Visible = false;
        }

        protected void Button1_Click(object sender, EventArgs e)
        { 
            if (dc.Products.Where(d => d.Name.ToLower().Equals(TextBoxName.Text.ToLower())).ToList().Count == 0)
            {
                if (checkImg(fileImg1, lblFile1) && checkImg(fileImg2, lblFile2) && checkImg(fileImg3, lblFile3))
                {
                    Product pro = new Product();
                    pro.SupplierID = Convert.ToInt32(this.ddlSupplier.SelectedValue);
                    pro.CategoryID = Convert.ToInt32(this.ddlCategory.SelectedValue);
                    pro.Name = this.TextBoxName.Text;
                    pro.Price = Convert.ToDouble(this.TextBoxPrice.Text);
                    pro.Warranty = Convert.ToInt32(this.TextBoxWarranty.Text);
                    pro.Status = Radio1.Checked ? Radio1.Checked : false;
                    pro.DateUpdate = DateTime.Now;
                    pro.Description = this.TextArea1Des.InnerText;
                    pro.View = 0;
                    this.dc.Products.InsertOnSubmit(pro);
                    this.dc.SubmitChanges();
                    insertIMG(fileImg1, lblFile1, pro);
                    insertIMG(fileImg2, lblFile2, pro);
                    insertIMG(fileImg3, lblFile3, pro);
                    Response.Redirect("ProductDetail.aspx?id=" + pro.ProductID.ToString());
                }
            }
            else
                lblMessage.Text = "this product is exists";

        }
        private void insertIMG(FileUpload file, Label lbl, Product pro)
        {
            try
            {
                if (file.HasFile)
                {
                    string filename = Path.GetFileName(file.FileName);
                    string[] arrfilename = filename.Split('.');
                    string tag = arrfilename[arrfilename.Length - 1];

                    filename = FormsAuthentication.HashPasswordForStoringInConfigFile(filename + DateTime.Now + new Random().Next(0, 10000), "sha1") + tag;
                    file.SaveAs(Server.MapPath(@"~/images/Product/") + filename);
                    lbl.Text = "File uploaded!";
                    //insert images to db
                    ImagesPro img = new ImagesPro();
                    img.ProductID = pro.ProductID;
                    img.NameImg = filename;
                    dc.ImagesPros.InsertOnSubmit(img);
                    dc.SubmitChanges();
                }
            }
            catch (Exception ex)
            {
                lbl.Text = "The file could not be uploaded. The following error occured: " + ex.Message;

            }
        }
        private bool checkImg(FileUpload file, Label lbl)
        {
            if (file.HasFile)
            {
                if (file.PostedFile.ContentType == "image/jpeg" || file.PostedFile.ContentType == "image/png" || file.PostedFile.ContentType == "image/gif")
                {
                    if (file.PostedFile.ContentLength < (102400 * 3))
                    {
                        return true;
                    }
                    else
                    {
                        lbl.Text = "The file has to be less than 300 kb!";
                        return false;
                    }
                }
                else
                {
                    lbl.Text = " upload *.jpeg,*.png,*.gif,*.jpg file!";
                    return false;
                }
            }
            return true;
        }




    }
}